package housing.task3;

import housing.task3.dao.HsAddressDao;
import housing.task3.dto.HsAddress;
import housing.task3.exceptions.HsAddressDaoException;
import housing.task3.factory.HsAddressDaoFactory;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;

import java.io.IOException;


/**
 * Created by IntelliJ IDEA.
 * User: Linderhof
 * Date: 23.11.2010
 * Time: 21:59:21
 * To change this template use File | Settings | File Templates.
 */
public class Main3 {
    private static final Logger logger =
            Logger.getRootLogger();

    public static void main(String[] args) {
        configureLogger();

        try {
            HsAddressDao addressDao = HsAddressDaoFactory.create();
            HsAddress[] result = addressDao.findAll();
            for (int i = 0; i < result.length; i++) {
                display(result[i]);
            }
            //deleting Address
            //addressDao.delete(1);

        } catch (HsAddressDaoException daoe) {
            daoe.printStackTrace();
        }
    }  //main()

    private static void configureLogger() {
        //BasicConfigurator.configure();
        final String logFilePath =
                ".\\timoshenko_housing\\src\\main\\logs\\Main3.log";
        try {
            FileAppender appenderTXT = new FileAppender(
                    new SimpleLayout(), logFilePath);
            /*   FileAppender appenderXML = new FileAppender(
          new XMLLayout(), "log.xml");*/

            logger.addAppender(appenderTXT);
/*
            logger.addAppender(appenderXML);
*/

        } catch (IOException ioe) {
            System.out.println("Failed create log file");
        }
    }

    /**
     * Method 'display'
     *
     * @param dto
     */
    public static void display(HsAddress dto) {
        StringBuffer buf = new StringBuffer();
        buf.append(dto.getAddressId());
        buf.append(", ");
        buf.append(dto.getStreet());
        buf.append(", ");
        buf.append(dto.getHouse());
        buf.append(", ");
        buf.append(dto.getFlat());
        System.out.println(buf.toString());
    }


}//class Main3
